package com.fr.lintcode;

import java.util.ArrayList;
import java.util.Stack;

import org.junit.Test;

/**
*作者：furong
*日期：2017年1月17日
*时间：下午6:31:27
*/
public class Q66 {
    /**
     * @param root: The root of binary tree.
     * @return: Preorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        if (root == null) {
            return new ArrayList<>(0);
        }
        ArrayList<Integer> rs = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            rs.add(node.val);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
        return rs;
    }

    @Test
    public void testA() {
        TreeNode root = new TreeNode(0);
        TreeNode r1 = new TreeNode(1);
        TreeNode r2 = new TreeNode(2);
        TreeNode r3 = new TreeNode(3);
        TreeNode r4 = new TreeNode(4);
        TreeNode r5 = new TreeNode(5);
        root.left = r1;
        root.right = r2;
        r1.left = r3;
        r2.right = r5;
        r3.right = r4;
        System.out.println(preorderTraversal(root));
    }
}
